<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-select v-model="listQuery.istatus" clearable filterable style="width:220px;margin-right: 10px;" class="filter-item" placeholder="全部状态">
        <el-option v-for="(item, index) in istatusOptions" :key="index" :label="item" :value="index"/>
      </el-select>
      <el-input v-model="listQuery.ip" clearable style="width:220px;margin-right: 10px;" class="filter-item" placeholder="精确IP查询"/>
      <el-select v-model="listQuery.ipSegment" clearable filterable style="width:220px;margin-right: 10px;" class="filter-item" placeholder="全部网段">
        <el-option v-for="(item, index) in ipSegmentOptions" :key="index" :label="item" :value="item"/>
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button :loading="scannerAllStatus" :disabled="scannerAllStatus" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-view" @click="handleScannerAll">扫描全部</el-button>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" element-loading-text="加载中" border fit highlight-current-row style="width: 100%">
      <el-table-column :label="$t('table.id')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="IP">
        <template slot-scope="scope">
          <el-button v-if="scope.row.istatus == 0" type="text" size="small" @click.native.prevent="goHost(scope.row.ip)">{{ scope.row.ip }}</el-button>
          <span v-else>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ istatusOptions[scope.row.istatus] }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新人">
        <template slot-scope="scope">
          <span>{{ scope.row.updatorName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.updateTime')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :loading="scannerStatus" :disabled="scannerStatus" type="primary" size="mini" @click="handleScanner(scope.row)">扫描</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { scanAllIP, scanSingleIP, fetchIPList, ipSegmentList } from '@/api/intranetIpPool'

export default {
  name: 'NetworkIpPool',
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      scannerAllStatus: false,
      scannerStatus: false,
      listQuery: {
        page: 1,
        limit: 20,
        istatus: null,
        ip: null,
        ipSegment: null
      },
      istatusOptions: ['占用', '可用'],
      ipSegmentOptions: []
    }
  },
  created() {
    this.getIpSegmentList()
    this.getIpList()
  },
  methods: {
    // 查询Ip段
    getIpSegmentList() {
      ipSegmentList().then(response => {
        this.ipSegmentOptions = response.data
      })
    },

    // 查询IP列表
    getIpList() {
      this.listLoading = true
      fetchIPList(this.listQuery).then(response => {
        this.list = response.data.records || ''
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getIpList()
    },
    // 设置每页条数
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getIpList()
    },
    // 设置页码
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getIpList()
    },
    // 扫描单个IP
    handleScanner(item) {
      this.scannerStatus = true
      scanSingleIP({ ip: item.ip }).then(response => {
        this.getIpList()
        if (response.success) {
          this.$notify({
            title: '成功',
            message: '扫描成功',
            type: 'success',
            duration: 2000
          })
        }
        this.scannerStatus = false
      })
    },
    // 扫描全部
    handleScannerAll() {
      this.scannerAllStatus = true
      scanAllIP().then(response => {
        this.getIpList()
        if (response.success) {
          this.$notify({
            title: '成功',
            message: '扫描成功',
            type: 'success',
            duration: 2000
          })
        }
        this.scannerAllStatus = false
      })
    },
    // 跳转到主机管理
    goHost(ip) {
      this.$router.push({
        path: 'host',
        query: {
          ip: ip
        }
      })
    }
  }
}
</script>
